package com.tanhua.dubbo.server.api;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Sound;
import com.tanhua.dubbo.server.pojo.Times;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;



@Service(version = "1.0.0")//对外提供服务
@Slf4j
public class SoundApiImpl implements SoundApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 发布语音
     *
     * @param sound
     * @return 保存成功后，返回语音id
     */
    @Override
    public String saveSound(Sound sound) {
        try {
            //校验
            if (!ObjectUtil.isAllNotEmpty(sound.getUserId(), sound.getSoundUrl())) {
                return null;
            }
            //设置id
            sound.setId(ObjectId.get());

            //发布时间
            sound.setCreated(System.currentTimeMillis());

            //保存到Mongodb中
            this.mongoTemplate.save(sound);
            return sound.getId().toHexString();//sound表的主键id
        } catch (Exception e) {
            //e.printStackTrace();这是打印在控制台，我们不希望打印在控制台，而是希望日志保存在本地文件中
            log.error("语音发布失败~ sound = " + sound, e);
        }
        return null;
    }

    /**
     * 根据用户id随机查询一条语音消息，排除用户本身
     * @param userId
     * @return
     */
    @Override
    public Sound receiveSound(Long userId) {
        //见RecommendUserApiImpl的排除包含方法
        Query query = Query.query(Criteria.where("userId").nin(userId)).with(Sort.by(Sort.Order.desc("created")));
        return this.mongoTemplate.findOne(query, Sound.class);
    }

    /**
     * 根据id查询剩余次数表
     *
     * @param userId
     * @return
     */
    @Override
    public Times queryTimesById(Long userId) {
<<<<<<< HEAD
<<<<<<< HEAD

=======
>>>>>>> adbd646be2b6f7d992a45aea78aead322dba44bb
=======
>>>>>>> adbd646be2b6f7d992a45aea78aead322dba44bb
        Query query = Query.query(Criteria.where("userId").is(userId));
        return this.mongoTemplate.findOne(query, Times.class);
    }

    /**
     * 根据id插入用户的剩余次数
     *
     * @param userId
     * @return
     */
    @Override
    public String insertTimesById(Long userId) {
        Times times = new Times();
        times.setId(ObjectId.get());
        times.setUserId(userId);
        times.setReceiveTimes(10);
        times.setUpdated(System.currentTimeMillis());
        this.mongoTemplate.save(times);
        return times.getId().toHexString();
    }

    /**
     * 根据id更新用户的剩余次数
     *
     * @param times
     * @return
     */
    @Override
    public Times updateReceiveTimes(Times times) {
        Query query = Query.query(Criteria.where("userId").is(times.getUserId()));
        Update update = Update.update("receiveTimes", times.getReceiveTimes());
        return this.mongoTemplate.findAndModify(query, update, Times.class);
    }

    /**
     * 同时跟新次数以及时间
     * @param times
     * @return
     */
    @Override
    public Times updateTimes(Times times) {
        Query query = Query.query(Criteria.where("userId").is(times.getUserId()));
        Update update1 = Update.update("receiveTimes", times.getReceiveTimes());
        this.mongoTemplate.findAndModify(query, update1, Times.class);
        Update update2 = Update.update("updated", times.getUpdated());
        return this.mongoTemplate.findAndModify(query, update2, Times.class);
    }

    /**
     * 根据id更新用户的剩余次数
     *
     * @param times
     * @return
     */
    /*@Override
    public UpdateResult updateReceiveTimes(Times times) {
        Query query = Query.query(Criteria.where("userId").is(times.getUserId()));
        Update update = Update.update("receiveTimes", times.getReceiveTimes());
        return this.mongoTemplate.updateFirst(query, update, Times.class);
    }*/
    /*由于import com.mongodb.client.result.UpdateResult没有实现序列化接口，故远程调用会报错，将代码改动或者将代码写在server模块*/
}
